package com.mcp.server.service;

import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HtmlUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.mcp.server.enums.DirectionLiteType;
import com.mcp.server.pojo.CityCoding;
import com.mcp.server.pojo.DirectionLiteVO;
import com.mcp.server.properties.McpProperties;
import com.mcp.server.utils.BaiduMapUtils;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

@Service
@Slf4j
public class BaiduMapService {

    @Resource
    McpProperties mcpProperties;

    private static final ConcurrentHashMap<String, CityCoding> CITY_CODING_MAP = new ConcurrentHashMap<>();

    public BaiduMapUtils baiduMapUtils;

    @PostConstruct
    public void CityCodingInitializing(){
        ClassPathResource classPathResource = new ClassPathResource("/static/AMap_adcode_citycode.xlsx");
        EasyExcel.read(classPathResource.getStream())
                .sheet("Sheet1")
                .head(CityCoding.class)
                .registerReadListener(new ReadListener<CityCoding>() {
                    @Override
                    public void invoke(CityCoding cityCoding, AnalysisContext analysisContext) {
                        CITY_CODING_MAP.put(cityCoding.getCityName(), cityCoding);
                    }

                    @Override
                    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                        log.info("行政区划编码构建成功");
                    }
                }).doRead();
    }

    @PostConstruct
    public void BaiduMapUtilsInitializing(){
        baiduMapUtils = new BaiduMapUtils(mcpProperties.getSecurityKey());
    }

    @Tool(name = "searchWeatherByCityName", description = "获取天气信息 需要提供完整城市名称 (例如：东乡区即可 无需江西省抚州市东乡区，只需要明确是省或者市或者区或者县)")
    public String searchWeatherByCityName(@ToolParam(description = "城市名称") String city) {
        CityCoding cityCoding = CITY_CODING_MAP.get(city);
        if (Objects.isNull(cityCoding)) {
            return "请你提供完整城市名称 (例如：东乡区即可 无需江西省抚州市东乡区，只需要明确是省或者市或者区或者县)";
        }
        return baiduMapUtils.getWeather(cityCoding.getCityId());
    }

    @Tool(name = "searchDirectionLiteByDriving",
            description = "驾车路线规划 根据起终点坐标规划驾车出行路线和耗时,支持10个途经点 支持设置偏好：常规路线、不走高速、躲避拥堵")
    public DirectionLiteVO searchDirectionLiteByDriving(
            @ToolParam(description = "起点城市名称 某某省某某市等") String originCityName,
            @ToolParam(description = "目的地城市名称 某某省某某市等") String destinationCityName,
            @ToolParam(description = "途经点名称 最多10个", required = false) List<String> waypoints,
            @ToolParam(description = "路线偏好 0：常规路线(时间最短) 1：不走高速 2：躲避拥堵 3：最短距离 4：花费最少 5：大路优先 6：表示避开轮渡", required = false) Integer tactics) {
        String result = baiduMapUtils.getDirectionLiteByDriving(originCityName, destinationCityName, waypoints, tactics);
        String url = baiduMapUtils.generateDirectionLiteMap(originCityName, destinationCityName, DirectionLiteType.DRIVING);
        return new DirectionLiteVO(result, url);
    }

    @Tool(name = "searchDirectionLiteByRiding", description = "骑行路线规划 根据起终点坐标规划骑行出行路线和耗时，支持普通自行车和电动自行车出行方式")
    public String searchDirectionLiteByRiding(
            @ToolParam(description = "起点城市名称 某某省某某市等") String originCityName,
            @ToolParam(description = "目的地城市名称 某某省某某市等") String destinationCityName,
            @ToolParam(description = "骑行类型 默认0 0：普通自行车 1：电动自行车") String ridingType) {
        return baiduMapUtils.getDirectionLiteByRiding(originCityName, destinationCityName, ridingType);
    }

    @Tool(name = "searchDirectionLiteByWalking", description = "步行路线规划 根据起终点坐标规划步行出行路线和耗时")
    public DirectionLiteVO searchDirectionLiteByWalking(
            @ToolParam(description = "起点城市名称 某某省某某市等") String originCityName,
            @ToolParam(description = "目的地城市名称 某某省某某市等") String destinationCityName) {
        String result = baiduMapUtils.getDirectionLiteByWalking(originCityName, destinationCityName);
        String url = baiduMapUtils.generateDirectionLiteMap(originCityName, destinationCityName, DirectionLiteType.WALKING);
        return new DirectionLiteVO(result,url);
    }

    @Tool(name = "searchDirectionLiteByTransit", description = "公交路线规划 根据起终点坐标规划同城公共交通出行路线和耗时，支持公交、地铁出行方式")
    public DirectionLiteVO searchDirectionLiteByTransit(
            @ToolParam(description = "起点城市名称 某某省某某市等") String originCityName,
            @ToolParam(description = "目的地城市名称 某某省某某市等") String destinationCityName) {
        String result = baiduMapUtils.getDirectionLiteByTransit(originCityName, destinationCityName);
        String url = baiduMapUtils.generateDirectionLiteMap(originCityName, destinationCityName, DirectionLiteType.TRANSIT);
        return new DirectionLiteVO(result,url);
    }


}
